package zhoug.zui.input;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import zhoug.common.adapters.recycler.layoutmanager.FlowLayoutManager;
import zhoug.common.beans.CharSequenceNameValue;
import zhoug.common.interfaces.NameTransformer;
import zhoug.zui.R;
import zhoug.zui.list.CheckListAdapter;
import zhoug.zui.list.OnSelectChangeListener;

/**
 * @Author: zhoug
 * @Date: 2024-07-12
 * @Description:
 */
public class ZUIInputCheckView extends ZUIInputView {
    protected RecyclerView mRecyclerView;
    protected CheckListAdapter<NameTransformer> mCheckListAdapter;


    public ZUIInputCheckView(Context context) {
        this(context, null);
    }

    public ZUIInputCheckView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, R.attr.ZUIInputCheckView_theme);
    }

    public ZUIInputCheckView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);

    }

    private void init(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ZUIInputCheckView, defStyleAttr, R.style.ZUIInputCheckView_def);
        boolean multiSelect = a.getBoolean(R.styleable.ZUIInputCheckView_zui_multiSelect, false);
        float valueSize = a.getDimension(R.styleable.ZUIInputCheckView_zui_valueSize, 0);
        int valueColor = a.getColor(R.styleable.ZUIInputCheckView_zui_valueColor, 0);
        int valueStyle = a.getInt(R.styleable.ZUIInputCheckView_zui_valueStyle, Typeface.NORMAL);
        int valueMaxLines = a.getInt(R.styleable.ZUIInputCheckView_zui_valueMaxLines, 0);
        int layoutType = a.getInt(R.styleable.ZUIInputCheckView_zui_layoutType, CheckListAdapter.TEXT);
        CharSequence[] entries = a.getTextArray(R.styleable.ZUIInputCheckView_android_entries);
        a.recycle();

        mCheckListAdapter = new CheckListAdapter<>(getContext()) {
            @Override
            public String getItemName(NameTransformer itemData) {
                return itemData.transformName();
            }
        };
        setMultiSelect(multiSelect);
        setValueSize(valueSize);
        setValueColor(valueColor);
        setValueStyle(valueStyle);
        setValueMaxLines(valueMaxLines);
        setLayoutType(layoutType);

        mCheckListAdapter.setEmptyViewEnable(false);
        mCheckListAdapter.setErrorViewEnable(false);
        mCheckListAdapter.setLoadMoreEnable(false);

        if (entries != null && entries.length > 0) {
            List<CharSequenceNameValue> data = new ArrayList<>();
            for (CharSequence item : entries) {
                data.add(new CharSequenceNameValue(item));
            }
            mCheckListAdapter.setData(new ArrayList<>(data));
        } else {
            mCheckListAdapter.setData(new ArrayList<>());
        }
        RecyclerView.LayoutManager layoutManager = new FlowLayoutManager(true);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mCheckListAdapter);
        setClearIconEnable(false);
        setIcon(null, 0);
    }

    @Override
    protected View createValueView(ViewGroup parent) {
        if (mRecyclerView == null) {
            mRecyclerView = new RecyclerView(getContext());
            mRecyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
            mRecyclerView.setHorizontalScrollBarEnabled(false);
            mRecyclerView.setVerticalScrollBarEnabled(false);
        }
        return mRecyclerView;
    }

    public <T extends NameTransformer> void setData(List<T> data) {
        if (data != null && !data.isEmpty()) {
            mCheckListAdapter.setData(new ArrayList<>(data));
        } else {
            mCheckListAdapter.setData(new ArrayList<>());
        }
    }

    public List<NameTransformer> getData() {
        return mCheckListAdapter.getData();
    }

    /**
     * 验证必填项
     */
    public void requiredValidate() {
        List<NameTransformer> select = getSelect();
        setError(select == null || select.isEmpty());
    }

    /**
     * 设置选择的数据
     *
     * @param select
     * @param <T>
     */
    public <T extends NameTransformer> void setSelect(List<T> select) {
        if (select != null && !select.isEmpty()) {
            mCheckListAdapter.setSelect(new ArrayList<>(select));
        } else {
            mCheckListAdapter.clearSelect();
        }
    }

    public <T extends NameTransformer> void setSelect(T select) {
        mCheckListAdapter.setSelect(select);
    }

    public void setCancelSelectEnabled(boolean cancelSelectEnabled) {
        mCheckListAdapter.setCancelSelectEnabled(cancelSelectEnabled);
    }


    public void clearSelect() {
        mCheckListAdapter.clearSelect();
    }

    /**
     * 获取选择的数据
     *
     * @return
     */
    public List<NameTransformer> getSelect() {
        return mCheckListAdapter.getSelect();
    }

    /**
     * 获取选择的数据
     *
     * @return
     */
    public NameTransformer getSingleSelect() {
        return mCheckListAdapter.getSingleSelect();
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        notifyDataChange();
    }

    @Deprecated
    @Override
    protected void onClearClick(View view) {

    }

    public void setOnSelectChangeListener(OnSelectChangeListener<NameTransformer> onSelectChangeListener) {
        mCheckListAdapter.setOnSelectChangeListener(onSelectChangeListener);
    }

    /**
     * 布局管理器
     *
     * @param layoutManager
     */
    public void setLayoutManager(RecyclerView.LayoutManager layoutManager) {
        if (mRecyclerView != null) {
            mRecyclerView.setLayoutManager(layoutManager);
        }
    }

    /**
     * 设置多选
     *
     * @param multiSelect
     */
    public void setMultiSelect(boolean multiSelect) {
        if (mClearImageView.getVisibility() == VISIBLE) {
            mClearImageView.setVisibility(GONE);
        }
        mCheckListAdapter.setMultiSelect(multiSelect);
    }

    /**
     * 设置布局样式
     *
     * @param layoutType {@link CheckListAdapter#RADIO_BUTTON,CheckListAdapter#CHECK_BOX,CheckListAdapter#TEXT}
     */
    public void setLayoutType(int layoutType) {
        mCheckListAdapter.setLayoutType(layoutType);
    }

    /**
     *
     */
    public void setIconRes(@DrawableRes int iconRes) {
        mCheckListAdapter.setIconRes(iconRes);
    }

    public void setIconTintRes(int iconTintRes) {
        mCheckListAdapter.setIconTintRes(iconTintRes);
    }

    public void setIconSize(int iconSize) {
        mCheckListAdapter.setIconSize(iconSize);
    }

    public void setValueColor(int valueColor) {
        mCheckListAdapter.setTextColor(valueColor);
    }

    public void setValueSize(float valueSize) {
        mCheckListAdapter.setTextSize(valueSize);
    }

    public void setValueStyle(int valueStyle) {
        mCheckListAdapter.setTextSize(valueStyle);
    }

    public void setValueMaxLines(int textMaxLines) {
        mCheckListAdapter.setTextMaxLines(textMaxLines);
    }


    public void notifyDataChange() {
        if (mCheckListAdapter != null) {
            mCheckListAdapter.notifyDataChange();
        }
    }

    public void notifyDataChange(boolean callSelectChange) {
        if (mCheckListAdapter != null) {
            mCheckListAdapter.notifyDataChange(callSelectChange);
        }
    }


}
